<!--
 * @Author: zhoulei0232
 * @Date: 2021-06-29 17:22:10
 * @LastEditTime: 2021-07-07 17:54:30
 * @LastEditors: zhoulei0232
 * @Description: 
-->
<template>
  <div class="sidebar">
    <div class="sidebar_head">
      <el-image style="width: 50px; height: 50px" :src="logoUrl"></el-image>
      <h5 class="title">vue3.0管理系统</h5>
    </div>

    <el-menu
      :uniqueOpened="true"
      default-active="/dashboard"
      class="sidebar-el-menu"
      @open="handleOpen"
      @close="handleClose"
      background-color="#324157"
      text-color="#fff"
      active-text-color="#20a0ff"
      router
    >
      <template v-for="item in sidebarList">
        <template v-if="item.subs">
          <el-submenu :index="item.index" :key="item.index">
            <template #title>
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-submenu
                v-if="subItem.subs"
                :index="subItem.index"
                :key="subItem.index"
              >
                <template #title>{{ subItem.title }}</template>
                <el-menu-item
                  v-for="(threeItem, i) in subItem.subs"
                  :key="i"
                  :index="threeItem.index"
                  >{{ threeItem.title }}</el-menu-item
                >
              </el-submenu>
              <el-menu-item
                v-else
                :index="subItem.index"
                :key="subItem.index"
                >{{ subItem.title }}</el-menu-item
              >
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index">
            <i :class="item.icon"></i>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script  lang="ts">
import { reactive, toRefs, computed } from "vue";
import { useRoute } from "vue-router";
import { useStore } from "@/store";
import logoUrl from "@/assets/img/nav-logo.png";

export default {
  name: "SideBar",
  setup() {
    const route = useRoute();
    const store = useStore();
    const state = reactive({
      logoUrl: logoUrl,
    });
    const sidebarList = computed(() => store.getters.getters_sidebarList);
    const onRoutes = computed(() => route.path.replace("/", ""));
    const handleOpen = (key: any, keyPath: any) => {
      console.log(key, keyPath);
    };
    const handleClose = (key: any, keyPath: any) => {
      console.log(key, keyPath);
    };
    return {
      ...toRefs(state),
      sidebarList,
      onRoutes,
      handleOpen,
      handleClose,
    };
  },
};
</script>

<style  lang="scss" scope>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  overflow-y: scroll;
  .sidebar_head {
    background-color: #324157;
    display: flex;
    padding: 0 20px;
    flex-direction: row;
    justify-content: flex-start;
    align-content: center;
    align-items: center;
  }
  .title {
    color: #fff;
    padding-left: 10px;
  }
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}
</style>